<template>
  <div class="chart-container">
    <div class="chart-header">
      <div class="chart-title">访问来源</div>
      <div class="chart-subtitle">用户注册方式分布</div>
    </div>
    <div ref="chartRef" class="chart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'UserSourceChart',
  data() {
    return {
      chart: null
    }
  },
  methods: {
    initChart() {
      if (!this.$refs.chartRef) return

      if (this.chart) {
        this.chart.dispose()
      }

      this.chart = echarts.init(this.$refs.chartRef)
      const option = {
        tooltip: {
          trigger: 'item',
          backgroundColor: 'rgba(255, 255, 255, 0.9)',
          borderColor: 'rgba(0, 0, 0, 0.1)',
          borderWidth: 1,
          padding: [8, 12],
          textStyle: {
            color: '#333'
          },
          formatter: '{b}: {c} ({d}%)'
        },
        legend: {
          bottom: '0%',
          left: 'center',
          itemWidth: 12,
          itemHeight: 12,
          textStyle: {
            color: '#666',
            fontSize: 12
          },
          itemGap: 20
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '45%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderColor: '#fff',
              borderWidth: 2,
              borderRadius: 4
            },
            label: {
              show: false
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 35, name: '账密注册', itemStyle: { color: '#409EFF' } },
              { value: 25, name: '邮箱注册', itemStyle: { color: '#67C23A' } },
              { value: 15, name: 'QQ登录', itemStyle: { color: '#E6A23C' } },
              { value: 15, name: 'Gitee登录', itemStyle: { color: '#8E44AD' } },
              { value: 10, name: 'Github登录', itemStyle: { color: '#F56C6C' } }
            ]
          }
        ]
      }

      this.chart.setOption(option)
    },
    handleResize() {
      if (!this.chart) return
      this.chart.resize()
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      window.addEventListener('resize', this.handleResize)
    })
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
    window.removeEventListener('resize', this.handleResize)
  }
}
</script>

<style lang="less" scoped>
.chart-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  .chart-header {
    padding: 20px 20px 0;
    flex-shrink: 0;

    .chart-title {
      font-size: 16px;
      font-weight: 500;
      color: #333;
      margin-bottom: 4px;
    }

    .chart-subtitle {
      font-size: 12px;
      color: #999;
    }
  }

  .chart {
    flex: 1;
    min-height: 0;
    padding: 0 10px;
  }
}

@media screen and (max-width: 768px) {
  .chart-container {
    .chart-header {
      padding: 16px 16px 0;

      .chart-title {
        font-size: 14px;
      }

      .chart-subtitle {
        font-size: 11px;
      }
    }

    .chart {
      padding: 0 5px;
    }
  }
}
</style> 